from __future__ import absolute_import
from . import message
from scopt.mpu.business.db import transaction
from scopt.util import message as msg_util
from scopt.util.message_types import INTEGER, STRING


def _request_model_mapping():
    return {'id': 'm01_add.id',
            'address_line_1': 'm01_add.line_1',
            'address_line_2': 'm01_add.line_2',
            'address_line_3': 'm01_add.line_3',
            'city': 'm01_add.city',
            'state': 'm01_add.state',
            'country': 'm01_add.country',
            'zip': 'm01_add.zip'}


@message(type='settings.address_create',
         version='0.1',
         reqd={'address_line_1': STRING(),
               'address_line_2': STRING(),
               'address_line_3': STRING(),
               'city': STRING(),
               'state': STRING(),
               'country': STRING(),
               'zip': STRING()},
         resd={'id': INTEGER(required=True)})
@transaction(['lds/BDS'])
def create(data_packet):
    db = msg_util.get_db(data_packet)
    reqd = msg_util.get_request(data_packet)['reqd']
    result = db.insert('m01_add',
                       values=reqd,
                       mapping=_request_model_mapping())
    return msg_util.insert_response(data_packet,
                                    result,
                                    create,
                                    {'id': result})


@message(type='settings.address_select',
         version='0.1',
         reqd={'id': INTEGER()},
         resd=[{'id': INTEGER(),
                'address_line_1': STRING(),
                'address_line_2': STRING(),
                'address_line_3': STRING(),
                'city': STRING(),
                'state': STRING(),
                'country': STRING(),
                'zip': STRING()}])
@transaction(['lds/BDS'])
def select(data_packet):
    db = msg_util.get_db(data_packet)
    reqd = msg_util.get_request(data_packet)['reqd']
    resd = db.query('m01_add',
                    where=db.and_(reqd, 'id'),
                    mapping=_request_model_mapping(),
                    result_mapper=_map_result)
    return msg_util.ok_response(data_packet, select, resd)


def _map_result(result):
    return [{'id':row['id'],
             'address_line_1':row['line_1'],
             'address_line_2':row['line_2'],
             'address_line_3':row['line_3'],
             'city':row['city'],
             'state':row['state'],
             'country':row['country'],
             'zip':row['zip']} for row in result]


@message(type='settings.address_update',
         version='0.1',
         reqd={'id': INTEGER(required=True),
               'address_line_1': STRING(),
               'address_line_2': STRING(),
               'address_line_3': STRING(),
               'city': STRING(),
               'state': STRING(),
               'country': STRING(),
               'zip': STRING()},
         resd={})
@transaction(['lds/BDS'])
def update(data_packet):
    db = msg_util.get_db(data_packet)
    reqd = msg_util.get_request(data_packet)['reqd']
    result = db.update_by_pk('m01_add',
                             reqd.get('id'),
                             values=reqd,
                             mapping=_request_model_mapping())
    return msg_util.update_response(data_packet, result, update)
